package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.DistributorProductsDto;
import com.qf.commerce.common.vo.DistributorProductsVo;
import com.qf.commerce.entity.DistributorProducts;
import com.qf.commerce.exception.DaoException;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.mapper.DistributorProductsMapper;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.DistributorProductsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

/*
 * @author 358
 * @since JDK 1.8
 * @data 2023/9/14 9:48
 */

@Service
public class DistributorProductsServiceImpl implements DistributorProductsService {

    @Resource
    private DistributorProductsMapper distributorProductsMapper;

    @Override
    public int updateByPrimaryKeySelective(DistributorProducts record) {
        return distributorProductsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DistributorProducts record) {
        return distributorProductsMapper.updateByPrimaryKey(record);
    }

    @Override
    public int addProducts(DistributorProductsDto dpd) {
        DistributorProducts dp = new DistributorProducts();
        if (ObjectUtils.isEmpty(dp) || ObjectUtils.isEmpty(dpd)) {
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        BeanUtils.copyProperties(dpd, dp);
        int num = distributorProductsMapper.insert(dp);
        if (num > 0) {
            return num;
        } else throw new DaoException(ResponseStatus.SYS_ERROR);
    }

    @Override
    public int updateProducts(DistributorProductsDto dpd) {
        DistributorProducts dp = new DistributorProducts();
        if (ObjectUtils.isEmpty(dp) || ObjectUtils.isEmpty(dpd)) {
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        BeanUtils.copyProperties(dpd, dp);
        dp.setUpdateTime(new Date());
        int num = distributorProductsMapper.updateByPrimaryKeySelective(dp);
        if (num > 0) {
            return num;
        } else throw new DaoException(ResponseStatus.SYS_ERROR);
    }

    @Override
    public PageInfo<DistributorProductsVo> listAll(Integer page, Integer size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> distributorProductsMapper.selectAll());
    }
}
